package com.rimi.bean;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;

public class JsonResult extends HashMap<String,Object> implements Serializable {

    public JsonResult append(String key,Object value){
        this.put(key,value);
        return this;
    }

    /**
     * 将当前对象转换为Json字符串
     * @return
     */
    public  String toJson(){
       return JSONObject.toJSONString(this);
    }

    public static JsonResult SUCCESS(){
        return new JsonResult(0,"操作成功",null);
    }

    public static JsonResult SUCCESS(String msg){
        return new JsonResult(0,msg,null);
    }

    public static JsonResult SUCCESS(Object data){
        return new JsonResult(0,"操作成功",data);
    }

    public static JsonResult SUCCESS(String msg,Object data){
        return new JsonResult(0,msg,data);
    }

    public static JsonResult ERROR(){
        return new JsonResult(500,"操作失败",null);
    }

    public static JsonResult ERROR(String msg){
        return new JsonResult(500,msg,null);
    }

    public static JsonResult ERROR(int code,String msg){
        return new JsonResult(code,msg,null);
    }

    private JsonResult(int code,String msg,Object data){
        this.put("code",code);
        this.put("msg",msg);
        this.put("data",data);
    }


}
